package com.example.hanfu_bk.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.example.hanfu_bk.pojo.Category;
import com.example.hanfu_bk.pojo.Style;
import com.example.hanfu_bk.service.StyleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j//记录日志
@CrossOrigin(origins = "*")

public class StyleController {
    private static final Logger log = LogManager.getLogger(StyleController.class);

    @Autowired
    private StyleService styleService;

    //加载数据
    @GetMapping("/cate/styles")
    public List<Style> searchAll(){
        log.info("查询全部");
        List<Style> styles=styleService.searchAll();
        return styles;
    }

    //查询
    @GetMapping("/cate/styles/search")
    public List<Style> searchCate( @RequestParam(required = false) String name,  @RequestParam(required = false) Integer typeId){
        log.info("按条件查询:{},{}",name,typeId);
        List<Style> styles;
        if(name==null){
            styles=styleService.searchCateByType(typeId);
        } else if (typeId==null) {
            styles=styleService.searchCateByName(name);
        } else {
            styles=styleService.searchCate(name,typeId);
        }
        return styles;
    }

    //删除
    @DeleteMapping("/cate/styles/{id}")
    public void deleteCate(@PathVariable Integer id){
        log.info("根据id删除{}", id);
        styleService.deleteCate(id);
    }

    //新增数据
    @PostMapping("/cate/styles")
    public void addCate(@RequestBody Style style){
        log.info("新增信息：{}", style);
        styleService.addCate(style);
    }

    //更新数据
    @PutMapping("/cate/styles")
    public void updateCate(@RequestBody Style style) {
        //log.info("更新汉服样式 ID: {}", style.id);
        styleService.updateCate(style);
    }

}